package org.example.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.example.common.R;
import org.example.common.Token;
import org.example.entity.vo.LoginVo;
import org.example.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.LoginException;

@RestController
public class LoginController{
    @Autowired
    private LoginService loginService;

//    @PostMapping("/login")
////    public R login(@RequestBody LoginVo loginVo){
////        try {
////            Token token=loginService.login(loginVo);
////            return R.ok(token);
////        } catch (LoginException e) {
////            return R.err(e.getMessage());
////        }
////    }

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        try {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginVo.getUsername(),loginVo.getPassword());
            SecurityUtils.getSubject().login(usernamePasswordToken);
        } catch (Exception e) {
            return R.err(e.getMessage());
        }
        return R.ok();
    }


    @GetMapping("/logout")
    public R logout(@RequestHeader String token){
        loginService.logout(token);
        return R.ok();
    }
}
